
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import javax.swing.*;


/**
 *
 */
public class GUI extends JFrame implements ActionListener
{
	private int keyNumber = 0;
	private String inputKey = "";
	private JButton button1;
	private JButton button2;
	private JLabel label1;
	private JLabel label2;
	private JLabel label4;
	private int numKeyStrokes = 0;
	private JFileChooser fileChoose;
	private JMenuItem openFile;
	private JMenuItem inputKeyNumber;
	private File textFile = null;
	private static JPanel centerPanel;
	private static JPanel panel;
	private double keyFrequency = 0;

	//This Constructor Sets up the basic layout of the buttons and the file input menu
	public GUI()
	{
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new GridLayout(5,1));
		button1 = new JButton("Reset/Stop");
		button1.addActionListener(this);
		leftPanel.add(button1);
		button2 = new JButton("Tune");
		button2.addActionListener(this);
		leftPanel.add(button2);
		label1 = new JLabel("Piano Key Number: ");
		leftPanel.add(label1);
		label4 = new JLabel("Piano Key Frequency: ");
		leftPanel.add(label4);
		label2 = new JLabel("File Path: ");
		leftPanel.add(label2);
		leftPanel.setPreferredSize(new Dimension(200,200));
		
		// west uses the panel's preferred width
		this.getContentPane().add(leftPanel, BorderLayout.WEST);
		fileChoose = new JFileChooser();
		JMenuBar menuBar = new JMenuBar();
		JMenu fileMenu = new JMenu("File");
		openFile = new JMenuItem("Open File");
		inputKeyNumber = new JMenuItem("Input Key Number");
		openFile.addActionListener(this);
		inputKeyNumber.addActionListener(this);

		fileMenu.add(openFile);
		fileMenu.add(inputKeyNumber);
		menuBar.add( fileMenu );
		this.getContentPane().add(menuBar, BorderLayout.NORTH);

	}
	
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == openFile) 
		{
			int returnVal = fileChoose.showOpenDialog(null);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				textFile = fileChoose.getSelectedFile(); 
				label2.setText("File Path: " + textFile.getName() );
			}
		}//if
		
		else if (e.getSource() == inputKeyNumber)
		{
			Frame frames[] = GUI.getFrames();
			try{
				String s = (String)JOptionPane.showInputDialog(
						frames[0],
						"Input Key Number",
						"Input Key Number",
						JOptionPane.PLAIN_MESSAGE
						);
				int typedNumber = Integer.parseInt(s);
				if (typedNumber > 0 && typedNumber < 89)
				{
					keyNumber = typedNumber;
					label1.setText("Piano Key Number: " + keyNumber);
					keyFrequency = getPianoKeyFrequency(keyNumber);
					label4.setText("Piano Key Frequency: "+keyFrequency);
					
				}
				else
				{
					JOptionPane.showMessageDialog(frames[0],
							"Invalid Key Number",
							"Error!",
							JOptionPane.ERROR_MESSAGE);
				}
			} catch( NumberFormatException ex )
			{
				JOptionPane.showMessageDialog(frames[0],
						"Invalid Key Number",
						"Error!",
						JOptionPane.ERROR_MESSAGE);
			}
		}//else if
		
		else if (e.getSource() == button2) 
		{	
			Frame frames[] = GUI.getFrames();
			if(keyFrequency<=0 || keyNumber < 0 || keyNumber > 88)
			{
					JOptionPane.showMessageDialog(frames[0] ,
					"Please Input Key Number!",
					"Error!",
					JOptionPane.ERROR_MESSAGE);
			}
			else if(textFile==null)
			{
				JOptionPane.showMessageDialog(frames[0] ,
						"Please Input Sound File!",
						"Error!",
						JOptionPane.ERROR_MESSAGE);
			}
			else
			{
				panel.removeAll();
				panel.add(new JLabel("Running.."));
				/*try {
					Thread.sleep(500);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}*/
				JOptionPane.showMessageDialog(frames[0] ,
						"Running..!",
						"",
						JOptionPane.INFORMATION_MESSAGE);
				panel.removeAll();
				panel.add( new DrawLine(1) );
				panel.updateUI();
			}
			
		}//else if
		else if (e.getSource() == button1) 
		{
			Frame frames[] = GUI.getFrames();
			JOptionPane.showMessageDialog(frames[0] ,
					"STOPPING",
					"Error!",
					JOptionPane.ERROR_MESSAGE);
			
		}//else if
		
	}//action performed
	
	private static void addComponent(Container container,Component c,int x,int y,int width,int height)
	{
		c.setBounds(x,y,width,height);
		container.add(c);
	}
	
	public static double getPianoKeyFrequency(int keyNum)
	{
		if (!(keyNum > 0 && keyNum < 89))
		{
			System.err.println("Error with input Piano Key!!");
		}
		
		int exp = keyNum - 49;
		
		DecimalFormat round = new DecimalFormat("0.00");
		
		double retVal = (Math.pow(Math.pow(2, 1.0 / 12),exp)*440);
		
		return Double.valueOf(round.format(retVal));
		
	}//getPianoKeyFrequency
	
	private static void createAndShow()
	{
		GUI frame = new GUI();
		frame.setTitle("WAV - CS229 Tuner");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		centerPanel = new JPanel();
		//label3 = new JLabel ("Tuning to Piano Key: ---");
		//centerPanel.add(label3);
		frame.getContentPane().add(centerPanel, BorderLayout.CENTER);
		panel = new JPanel();
		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//panel.add(label3);
		frame.getContentPane().add(panel,BorderLayout.CENTER);
		panel.setBackground(new Color(220,220,220));
		
		//Instantiate DFT Class
		//Call DFT Algorithm
		//Draw line based on algorithm
		
		panel.add( new DrawLine(0) );
		
		//addComponent(frame, panel, 10,10,240,50);
		// the pack() method sizes the frame to "fit" the components
		frame.setSize(800, 300);
		//frame.pack();
		frame.setVisible(true);
		frame.setResizable(false);
	}

	public static void main(String[] args)
	{
		Runnable r = new Runnable()
		{
			public void run()
			{
				createAndShow();
			}
		};
		SwingUtilities.invokeLater(r);
	}
	
}//class GUI

class DrawLine extends JPanel
{
		int line;
	public DrawLine(int i)
	{line = i;}
	
	public Dimension getPreferredSize()
	{
		return new Dimension(250, 250);
	}
	
	protected void paintComponent(Graphics g)
	{
 
		g.setColor( Color.black );
		// X Start, Y Start, X End, Y End
		// X = <---------->
		if(line == 0)
		g.drawLine ( 125, 0, 125, 225 );
		if(line > 0)
		g.drawLine ( 125, 225, 175, 0 );
		if(line < 0)
		g.drawLine ( 125, 225, 75, 0 );
 
	}

}//class